package pratice.learn.flow;

import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 学习流程变量
 * @author 吴宇亮 on 2019/5/15 0015 上午 10:50
 */
public class ProcessVariable {

    public static ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault().buildProcessEngine();

    /**
     * 定义流程
     */
    @Test
    public void processDef(){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        repositoryService.createDeployment()
                .addClasspathResource("activiti/template/LeaveBill.bpmn")
                .addClasspathResource("activiti/template/LeaveBill.png")
                .name("请假流程")
                .deploy();
    }

    /**
     * 启动流程，并且添加流程变量
     */
    @Test
    public void runProcess(){
        RuntimeService runtimeService = processEngine.getRuntimeService();
        Map<String, Object> variablesMap = new HashMap<>();
        variablesMap.put("请假天数", 2);
        variablesMap.put("请假原因", "不想上班");
        variablesMap.put("请假时间", LocalDateTime.now());

        ProcessInstance leaveBill = runtimeService.startProcessInstanceByKey("LeaveBill", variablesMap);
    }

    /**
     * 设置流程变量
     */
    @Test
    public void setVariable(){
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //runtimeService.setVariable("20001", "请假人", "李四");

        /*
        Map<String, Object> variablesMap = new HashMap<>();
        variablesMap.put("请假天数", 2);
        variablesMap.put("请假原因", "去旅游");
        variablesMap.put("请假时间", LocalDateTime.now());
        runtimeService.setVariables("20001", variablesMap);
        */

        TaskService taskService = processEngine.getTaskService();
        taskService.setVariable("20009", "申请变量","测试");
    }

    /**
     * 获取流程变量
     */
    @Test
    public void getVariable(){
        RuntimeService runtimeService = processEngine.getRuntimeService();
        LocalDateTime time = runtimeService.getVariable("20001", "请假时间", LocalDateTime.class);
        System.out.println(time);

        String reason = runtimeService.getVariable("20001", "请假原因", String.class);
        System.out.println(reason);
    }

    /**
     * 查询历史流程变量
     */
    public void getHisVariable(){
        HistoryService historyService = processEngine.getHistoryService();
        historyService.createHistoricVariableInstanceQuery();
    }

}
